package com.xiaoniu.lib_keleim

import android.app.Service
import android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import android.os.Binder
import android.os.IBinder
import android.os.Parcel


/**
 * 长链接服务
 */
class SocketServer : Service() {

    private val mBinder: SocketBinder = SocketBinder()

    override fun onBind(intent: Intent?): IBinder {
        return mBinder
    }

    override fun onCreate() {
        super.onCreate()
    }
}


class SocketBinder : Binder() {

    override fun onTransact(code: Int, data: Parcel, reply: Parcel?, flags: Int): Boolean {
        return super.onTransact(code, data, reply, flags)
    }

    fun sendMessage(message: ByteArray) {
    }
}

//监视服务的状态
class MyConnection : ServiceConnection {
    var myBinder: SocketBinder? = null
    override fun onServiceConnected(name: ComponentName, service: IBinder) {
        myBinder = service as SocketBinder
    }

    //失去连接
    override fun onServiceDisconnected(name: ComponentName) {
        myBinder = null
    }

    fun sendMessage(message: ByteArray) {
        myBinder?.sendMessage(message)
    }
}